﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PNProject.Core;
using PNProject.Core.Converter;

namespace PNProject.Controllers
{
    [Authorize]
    public class AdminSupportController : Controller
    {
        //
        // GET: /AdminSupport/

        public ActionResult Index()
        {
            var list = PublicService.GetAllSupport();
            SupportContract contract = new SupportContract();
            contract.Title = "Danh sách nick hỗ trợ";
            contract.Data = list;
            return View(contract);
        }

        
        public ActionResult Create()
        {
            return View(new SupportModel());
        }

        [HttpPost]
        public ActionResult Create(SupportModel model)
        {
            var obj = SupportConverter.ToSupport(model);
            StaticData.supportRepo.Save(null, obj);
            return RedirectToAction("Index");
        }

        public ActionResult Edit(string id)
        {
            try
            {
                int newid = int.Parse(id);
                var dbo = StaticData.supportRepo.GetOne(a => a.SupportID == newid);
                var model = SupportConverter.SupportTo(dbo);
                return View(model);
            }
            catch (Exception e)
            {
                return RedirectToAction("Index");
            }
            
        }

        [HttpPost]
        public ActionResult Edit(SupportModel model)
        {
            try
            {
                int newsid = int.Parse(model.ID);
                var obj = StaticData.supportRepo.GetOne(a => a.SupportID == newsid);
                var newobj = SupportConverter.ToSupport(model);
                newobj.SupportID = obj.SupportID;
                StaticData.supportRepo.Save(obj, newobj);
                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                return RedirectToAction("Index");
            }

        }

        public ActionResult Delete(string id)
        {
            try
            {
                int newid = int.Parse(id);
                var dbo = StaticData.supportRepo.GetOne(a => a.SupportID == newid);
                StaticData.supportRepo.Delete(dbo);
            }
            catch (Exception e)
            {

            }
            return RedirectToAction("Index");
        }
    }
}
